<?php
	class TeamManageAction extends Action {
		public function index(){
			$this->display();
		}
		public function team(){
			$model=M();
			$jurypro=M('juryproject');
			$jury=M('jury');
			$where="";
			$keyword1=$this->_post('keyword1');
			$keyword2=$this->_post('keyword2');
			$term1=$this->_post('term1');
			$term2=$this->_post('term2');
			if(!empty($keyword1)){
				$where='t.Name like'."'%".$keyword1."%' and ";	//组合查询语句
				$this->assign('keyword1',$keyword1);
			}
			if(!empty($keyword2)){
				$where='t.SchoolName like'."'%".$keyword2."%' and ";	//组合查询语句
				$this->assign('keyword2',$keyword2);
			}
			if(!empty($term1)){
				$where='t.Status ='.$term1.' and ';				//组合查询语句
				$this->assign('term1',$term1);
			}
		
			if(!$term2 == 0){
			
				$stu=M('student');
				$slist=$stu->field('UserId')->where('EduLevel='."'".$term2."'")->select();
				for($i=0;$i<count($slist);$i++){
					$sarrid[]=$slist[$i]['UserId'];
				}
				$sidstr=implode(',',$sarrid);
				$where="Captain in (".$sidstr.') and ';
			}
			if($_POST['numPerPage']){
				$pagesize = $this->_post('numPerPage');
				$_GET['page'] = $this->_post('pageNum');//自定义翻页类需要 GET （当前页）信息
			}else{
				$pagesize=2;
			}
			$totalCount=$model->table('team t,project p')->where($where.'t.Id=p.TeamId')->count();
			$this->assign('totalCount',$totalCount);				//把总数发给模板
			import('@.Action.Page');
			$page = new Page($totalCount,$pagesize);				//实例化分页类
			$pageinfo = $page->getPageInfo();						//获取分页
			$this->assign('currentPage',$pageinfo['page']);			//分配当前页数
			$this->assign('pagesize',$pagesize);					//分配一页显示的条数
			$tarr=$model->table('team t,project p')->field('t.Id,t.Name,t.CaptainName,t.CraeteDate,t.Declaration,t.Fetion,t.Poll,t.SchoolName,t.Score,t.Status,p.Id as Pid')->limit($pageinfo['offset'],$pageinfo['num'])->where($where.'t.Id=p.TeamId')->select();
			$tnum=count($tarr);
			for($j=0;$j<$tnum;$j++){
				$jarr=$jurypro->field('jury,ThemeScore,CreativeScore,PublicScor')->where('ProjectId='.$tarr[$j]['Pid'].' and Section='.$tarr[$j]['Status'])->select();
				$total=0;
				$num=count($jarr);
				for($i=0;$i<$num;$i++){
					$yarr=$jury->field('TrueName')->where('UserId='.$jarr[$i]['jury'])->find();
					$n=$jarr[$i]['ThemeScore']+$jarr[$i]['CreativeScore']+$jarr[$i]['PublicScor'];
					$total+=$n;
					$tarr[$j]['jury'.$i]=$yarr['TrueName'];
					$tarr[$j]['Pscre'.$i]=$n;
				}
				$tarr[$j]['Total']=$total;
			}
			$this->assign('arr',$tarr);
			$this->display();
		} 
		public function up(){
			$team=M('team');
			$id=$this->_get('tid');
			$status=$this->_get('sid');
			$data['Status']=$status+1;
			if($team->where('Id = '.$id)->save($data)){
				$this->success_ajax('晋级成功');
			}else{
				$this->error_ajax('晋级失败');
			}
		}
		public function edit(){
			$id=$this->_get('tid');
			$team=M('team');
			$teamm=M(' teammembers');
			$high=M('highschool');
			$harr=$high->field('Id,SchoolName')->select();
			$tmarr=$teamm->field('UserId,TrueName')->where('IsApproved=1 and TeamId='.$id)->select();
			$list=$team->field('Id,Name,CaptainName,Captain,Declaration,SchoolId,SchoolName,Status,Fetion')->where('Id = '.$id)->find();
			$tn=count($tmarr);
			$hn=count($harr);
			for($i=0;$i<$tn;$i++){
				if($tmarr[$i]['UserId']==$list['Captain']){
					$tmarr[$i]['sta']=1;
				}else{
					$tmarr[$i]['sta']=0;
				}
			}
			for($i=0;$i<$hn;$i++){
				if($harr[$i]['Id']==$list['SchoolId']){
					$harr[$i]['sta']=1;
				}else{
					$harr[$i]['sta']=0;
				}
			}
			$this->assign('tmarr',$tmarr);
			$this->assign('list',$list);
			$this->assign('harr',$harr);
			$this->display();
		}
		public function do_edit(){
			$id=$this->_post('tid');
			$data['Name']=$this->_post('Name');
			$data['Captain']=$this->_post('Captain');
			$data['Declaration']=$this->_post('Declaration');
			$data['Fetion']=$this->_post('Fetion');
			$data['SchoolId']=$this->_post('SchoolId');
			$data['Status']=$this->_post('Status');
			$high=M('highschool');
			$user=M('users');
			$team=M('team');
			$slist=$high->field('SchoolName')->where('Id='.$data['SchoolId'])->find();
			$data['SchoolName']=$slist['SchoolName'];
			$ulist=$user->field('TrueName')->where('UserId='.$data['Captain'])->find();
			$data['CaptainName']=$ulist['TrueName'];
			if($team->where('Id='.$id)->save($data)){
				$this->success_ajax('修改成功');
			}else{
				$this->error_ajax('修改失败');
			}
		}
	}
